package com.amphora.services.crud.impl;

import java.util.HashMap;

import com.amphora.model.Category;
import com.amphora.model.SystemProperty;
import com.amphora.services.crud.SystemPropertyService;
import com.amphora.util.exceptions.CRUDExceptions;
import com.amphora.util.factories.DomainFactory;
import com.amphora.util.repository.JPAUtil;

public class SystemPropertyServiceImpl implements SystemPropertyService {

	@Override
	public void createSystemSetting(HashMap<String, String> stringValuesForSystemProperty) throws CRUDExceptions{
		// TODO Auto-generated method stub
		SystemProperty newSystemProperty = DomainFactory.getSystemProperty(stringValuesForSystemProperty.get("name"), stringValuesForSystemProperty.get("value"));
		JPAUtil.createEntity(newSystemProperty);
		
		if(JPAUtil.findById(Category.class, newSystemProperty.getId())==null)
			throw new CRUDExceptions("Error: There was a error while creating the system property");
	}

	@Override
	public void updateSystemSetting(SystemProperty systemPropertyToUpdate) throws CRUDExceptions{
		// TODO Auto-generated method stub
		SystemProperty systemPropertyToBeUpdated = JPAUtil.findById(SystemProperty.class, systemPropertyToUpdate.getId());
		
		if(JPAUtil.findById(Category.class, systemPropertyToUpdate.getId())==null)
			throw new CRUDExceptions("Error: There was a error while reading the System Property from the database");
		
		JPAUtil.update(SystemProperty.class, systemPropertyToBeUpdated.getId());
	}

	@Override
	public void deleteSystemSetting(Long systemPropertyId) throws CRUDExceptions{
		// TODO Auto-generated method stub
		SystemProperty systemPropertyToBeDeleted = JPAUtil.findById(SystemProperty.class, systemPropertyId);
		
		if(JPAUtil.findById(SystemProperty.class, systemPropertyToBeDeleted.getId())==null)
			throw new CRUDExceptions("Error: There was a error while reading the System Property from the database");
		
		JPAUtil.delete(systemPropertyToBeDeleted);
		
		if(JPAUtil.findById(SystemProperty.class, systemPropertyToBeDeleted.getId())!=null)
			throw new CRUDExceptions("Error: There was a error while deleting the System Property from the database");
	}

}
